package com.phgame.newsmth.pploader;

import java.lang.ref.WeakReference;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Bitmap;


import android.os.Build;
import android.util.Log;
import android.util.LruCache;


@SuppressLint("NewApi") public class MemCache {
	private LruCache<String,Bitmap> mMemCache;
	
	private int CACHE_SIZE = 10*1024*1024;
	public MemCache(){
		init(0);
	}
	public MemCache(int size){
		init(size);
	}
	private void init(int size){
		int cachesize = CACHE_SIZE;
		if(size > 0){
			cachesize = size;
		}
		mMemCache = new LruCache<String,Bitmap>(cachesize){

			@Override
			protected int sizeOf(String key, Bitmap value) {
				
				return value.getByteCount();
			}

			
		};
	}
	
	
	public Bitmap getDrawble(String url) {
		Log.d("!~~~~~~~~~~~", "getDrawble ~~~~~~~" + mMemCache.toString() + mMemCache.size() + " url " + url + " url bitmap " + mMemCache.get(url));
		
		if (mMemCache.get(url) != null) {

			return mMemCache.get(url);

		}
		return null;

		// }
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) 
	public void addCache(String url, Bitmap bitmap) {
		Log.d("!~~~~~~~~~~~", "addCache ~~~~~~~" + mMemCache.toString() + mMemCache.size() + " url " + url + " url bitmap " + mMemCache.get(url));
		if (url != null){
			//WeakReference<Bitmap> bitmap_ref = new WeakReference<Bitmap>(bitmap);
			mMemCache.put(url, bitmap);
		}
	}
}
